<?php
$GLOBALS['url']=Yii::app()->request->baseUrl;
class MainController extends Controller
{
	public $layout= '/layouts/index.php';
	public function actionIndex()
	{
		$this->layout='index';
		$product=new Product();
		$product=$product->findAll();
		
		$this->render('index',array('products'=>$product));
	}
	public function actionAddProduct()
	{
		$this->layout='index';
		$this->render('AddProduct');
	}
	public function actionLogout()
	{
		if($_SESSION!=null)
			$_SESSION->clear();
		return Redirect("/home/index");
	}
	public function actionsaveproduct()
	{
		$this->layout='index';
		$model = new Product();
		
		$model->name=$_POST['t1'];
		$model->catogary=$_POST['t2'];
		$model->description=$_POST['t3'];
		$model->url='/css/images/'.$_FILES["file"]["name"];
		
		//$tmp_name = $_FILES["pictures"]["tmp_name"];
		if(move_uploaded_file($_FILES["file"]["tmp_name"],'css/images/'.$_FILES["file"]["name"]))
			print_r(Yii::app()->request->baseUrl.$model->url);
		else 
			print_r("hello");
		$model->save();
		$product=new Product();
		$product=$product->findAll();
		$this->render('index',array('products'=>$product));
	}
	public function actionsignup()
	{
		$this->layout='index';
		$this->renderPartial('signup');
	}
	public function actionsignin()
	{
		$this->layout='index';
		$this->render('signin');
	}
	public function actionverifyuser()
	{
		$this->layout='index';
		$user = new Users();
		$user->username=$_POST['t1'];
		$user->password=$_POST['t2'];
		$user1=$user->find('username=:username'AND 'password=:password',array(':username'=>$user->username,':password'=>$user->password));
		if($user1)
		{
			$product=new Product();
			$product=$product->findAll();
			$this->render('index',array('products'=>$product));
		}
			
		else 
			echo('signin');
	}
	public function actionsaveuser()
	{
		$this->layout='index';
		$model = new Users();
		$message='Error!';
		//print_r($_POST);
		//exit();
		$model->username=$_POST['t1'];
		$model->password=$_POST['t2'];
		$model->save();
		$this->render('signin');
	}
	public function actionRegister()
	{
		$this->layout='index';
		$model=new Users('register');
	
		// uncomment the following code to enable ajax-based validation
		/*
		 if(isset($_POST['ajax']) && $_POST['ajax']==='users-register-form')
		 {
		echo CActiveForm::validate($model);
		Yii::app()->end();
		}
		*/
	
		if(isset($_POST['Users']))
		{
			$model->attributes=$_POST['Users'];
			if($model->validate())
			{
				// form inputs are valid, do something here
				return;
			}
		}
		$this->render('register',array('model'=>$model));
	}
	

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}